import { UserAPI } from "@/http/api";
import { defineStore } from "pinia";
//仓库键名必须是use开头并且具有唯一性
import { userInfo } from "./keys";

interface IStore {
  userInfo: userStoreInfo;
  carTotal: number;
}
interface userStoreInfo {
  headImg: string;
  nickName: string;
  coin: number;
}
//管理用户未登录/登录信息
export const userInfoStore = defineStore("userInfo", {
  state(): IStore {
    return {
      userInfo: {
        headImg: new URL("@/assets/img/service.848ec511.png", import.meta.url)
          .href, //头像
        nickName: "昵称", // 昵称
        coin: 0, //积分
      },
      carTotal: 0, //购物车数量
    };
  },
  getters: {},
  actions: {
    //修改登录状态
    changeUserInfo(payload: any) {
      this.userInfo = payload.userInfo;
      this.carTotal = payload.cartTotal;
    },
    //更新用户信息
    UserInfoList() {
      UserAPI().then((res) => {
        if (res.code === 0) {
          this.changeUserInfo(res.data);
        }
      });
    },
    //修改回默认信息
    initUserInfo() {
      (this.userInfo = {
        headImg: new URL("@/assets/img/service.848ec511.png", import.meta.url)
          .href, //头像
        nickName: "昵称", // 昵称
        coin: 0, //积分
      }),
        (this.carTotal = 0); //购物车数量
    },
  },
});
